//
//  THJImgPickerViewController.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/28.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJImgPickerViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "THJImgPicekerCell.h"

@interface THJImgPickerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    
        ALAssetsLibrary *library;
    UICollectionView * _collectionView;
    THJImgPicekerCell * cell;
    BOOL tempSign;
}

@property (nonatomic,strong)NSMutableArray *array;
@end

@implementation THJImgPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCollectionView];
    //创建可变数组,存储资源文件
    _array = [NSMutableArray array];
    //创建资源库,用于访问相册资源
    library = [[ALAssetsLibrary alloc] init];
    //遍历资源库中所有的相册,有多少个相册,usingBlock会调用多少次
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        //如果存在相册,再遍历
        if (group) {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                
                if (result) {
                    [_array addObject:result];
                }
            }];
        }
        
        //刷新_collectionView reloadData;
        [_collectionView reloadData];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"访问失败");
    }];
    // Do any additional setup after loading the view from its nib.
}
-(void)setCollectionView{
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:[self creatLayout]];
    _collectionView.backgroundColor = SingleColor(whiteColor);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
     //[_collectionView registerClass:[THJImgPicekerCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"THJImgPicekerCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
}
-(UICollectionViewFlowLayout*)creatLayout{
    /** 创建布局类对象*/
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    /** 设置item的大小 默认值：(50, 50)*/
    flowLayout.itemSize = CGSizeMake((ScreenWidth-40)/3,(ScreenWidth-40)/3);
    /** 设置滚动方向*/
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    /** 设置头部视图的大小*/
    //flowLayout.headerReferenceSize = CGSizeMake(100, 50);
    /** 设置尾部视图的大小*/
    //flowLayout.footerReferenceSize = CGSizeMake(200, 100);
    /** 设置两列之间间距的最小值*/
    flowLayout.minimumInteritemSpacing = 10;
    /** 设置两行之间间距的最小值*/
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    return flowLayout;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"count:%ld",_array.count);
    return _array.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = SingleColor(brownColor);
    //取出对应的资源数据
    ALAsset *result =_array[indexPath.row];
    UIImage *image = [UIImage imageWithCGImage:result.thumbnail];
    cell.bgImgv.image = image;
    [cell.btn setImage:kGetImage(@"ico_未选中") forState:UIControlStateNormal];
     [cell.btn setImage:kGetImage(@"ico_选中") forState:UIControlStateSelected];
    return cell;

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    cell.btn.selected = !cell.btn.selected;
   // [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
@end
